/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ipcalculator;

import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author usuario
 */
public class VisualMIDlet extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;
    int y = 0;
    int a = 0;
    public   String[] splitIP;
     public  String[] splitMascara;
    public   String[] splitNet;
       public         long BroadCast = 0;
       public         long Net;
       public         long Net2 = 0;
       public         String NetID ="";
       public         String Broad ="";
        public        long Mascara;
       public         long countrede;
       public         long mascaraElevada;
       public         int ultimaCasamMsk = 0;
       public         String ip; 
       public         String MascaraRede;
       public        String mask = "";
       public long p;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form form;
    private TextField textField;
    private StringItem stringItem;
    private StringItem stringItem1;
    private StringItem stringItem2;
    private TextField textField1;
    private Command okCommand;
    private Command backCommand;
//</editor-fold>//GEN-END:|fields|0|
    /**
     * The VisualMIDlet constructor.
     */
    public VisualMIDlet() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display; if
     * <code>null</code>, then
     * <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initialized instance of form component.
     *
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form("form", new Item[]{getTextField(), getTextField1(), getStringItem(), getStringItem1(), getStringItem2()});//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getOkCommand());
            form.addCommand(getBackCommand());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initialized instance of textField component.
     *
     * @return the initialized component instance
     */
    public TextField getTextField() {
        if (textField == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            textField = new TextField("Entre com o ip:", null, 32, TextField.ANY);//GEN-LINE:|16-getter|1|16-postInit
            
        }//GEN-BEGIN:|16-getter|2|
        return textField;
    }
//</editor-fold>//GEN-END:|16-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|17-getter|0|17-preInit
    /**
     * Returns an initialized instance of stringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
            stringItem = new StringItem("Mascara:", null);//GEN-LINE:|17-getter|1|17-postInit
            // write post-init user code here
        }//GEN-BEGIN:|17-getter|2|
        return stringItem;
    }
//</editor-fold>//GEN-END:|17-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initialized instance of stringItem1 component.
     *
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("Net ID:", null);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return stringItem1;
    }
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem2 ">//GEN-BEGIN:|19-getter|0|19-preInit
    /**
     * Returns an initialized instance of stringItem2 component.
     *
     * @return the initialized component instance
     */
    public StringItem getStringItem2() {
        if (stringItem2 == null) {//GEN-END:|19-getter|0|19-preInit
            // write pre-init user code here
            stringItem2 = new StringItem("BroadCast:", null);//GEN-LINE:|19-getter|1|19-postInit
            // write post-init user code here
        }//GEN-BEGIN:|19-getter|2|
        return stringItem2;
    }
//</editor-fold>//GEN-END:|19-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        
        if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|24-preAction
            if (command == backCommand) {//GEN-END:|7-commandAction|1|24-preAction
                // write pre-action user cod
               
                exitMIDlet();//GEN-LINE:|7-commandAction|2|24-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|3|22-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|4|22-postAction
             //defini a mascara
                
                        
                MascaraRede = textField1.getString();
                countrede = Integer.parseInt(MascaraRede);
                int contador =0;
                int fim = 0;
                do{
                   if(countrede==0){
                       fim=1;
                   }
                    if(fim==1){
                        mask +="0.";
                        contador++;
                    }else{
                  if(countrede >=8){
                     mask +="255.";
                     countrede = countrede - 8;
                     contador++;
                  }
                  else{
                      for(int x = 0; x < countrede;x++){
                   if(x==0){
                   y = 128;
                   a = 128;
                   ultimaCasamMsk = ultimaCasamMsk +y;
                   }
                   else{
                   a = a/2 ;   
                   
                   ultimaCasamMsk = ultimaCasamMsk +a;
                   fim = 1;
                   }         
               }
                      mask +=ultimaCasamMsk; 
                      contador++;
                  }}
                }while(contador<4);
               System.out.println("IP:"+mask);
                stringItem.setText(""+mask);
                
                
              //separa o ip  para calcular
                ip = textField.getString();
                String[] lines = this.split(ip,"<br>");
 for(int i = 0; i < lines.length; i++) 
 {
      splitIP = this.split(lines[i],".");
      System.out.println("IP:"+splitIP[0] + " " + splitIP[1] + " " + splitIP[2] + " "+splitIP[3]);     
 }
 //separa a mascara
 String[] line = this.split(mask,"<br>");
 for(int i = 0; i < line.length; i++) 
 {
      splitMascara = split(line[i],".");
      System.out.println("Mascara:"+splitMascara[0] + " " + splitMascara[1] + " " + splitMascara[2]+ " "+splitMascara[3]);     
 }
 //define o Net ID
 int p = Math.abs(12/5);
 System.out.println("P=="+p);
 for(int z =0;z <4;z++){
     System.out.println("FOR");
     p = 256-Integer.parseInt(splitMascara[z]);
     System.out.println("valor de P:"+p);
     Net = Integer.parseInt(splitIP[z])/p;
     System.out.println("valor de Net1:"+Net);
     Net2 = Math.abs(Net)*p;
     System.out.println("valor de Net2:"+Net2);
     NetID += Net2+".";   
     System.out.println("NetID: :"+NetID);
 }
 stringItem1.setText(NetID);
 
 //separa a NetID
 String[] n = this.split(NetID,"<br>");
 for(int i = 0; i < n.length; i++) 
 {
      splitNet = this.split(n[i],".");
      System.out.println("NetID: "+splitNet[0] + " " + splitNet[1] + " " + splitNet[2]+ " "+splitMascara[3]);     
 }
 
 
 //define o BroadCast
 for(int z =0;z<4;z++){
     BroadCast = 255 - Integer.parseInt(splitMascara[z]) + Integer.parseInt(splitNet[z]);
     Broad += BroadCast+".";
             }
 stringItem2.setText(Broad);
 System.out.println("Mascara: "+mask);
 System.out.println("NetID: "+NetID);
 System.out.println("BroadCast: "+Broad);
 mask="";
 NetID="";
 Broad="";
 
                
                
                        
            }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
        }//GEN-END:|7-commandAction|5|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|6|
//</editor-fold>//GEN-END:|7-commandAction|6|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initialized instance of okCommand component.
     *
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|21-getter|1|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return okCommand;
    }
//</editor-fold>//GEN-END:|21-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initialized instance of backCommand component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            backCommand = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|23-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField1 ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initialized instance of textField1 component.
     *
     * @return the initialized component instance
     */
    public TextField getTextField1() {
        if (textField1 == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            textField1 = new TextField("Mascara de rede:", null, 32, TextField.ANY);//GEN-LINE:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return textField1;
    }
//</editor-fold>//GEN-END:|27-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
    private String[] split(String original,String separator) {
    Vector nodes = new Vector();
    // Parse nodes into vector
    int index = original.indexOf(separator);
    while(index >= 0) {
        nodes.addElement( original.substring(0, index) );
        original = original.substring(index+separator.length());
        index = original.indexOf(separator);
    }
    // Get the last node
    nodes.addElement( original );

     // Create split string array
    String[] result = new String[ nodes.size() ];
    if( nodes.size() > 0 ) {
        for(int loop = 0; loop < nodes.size(); loop++)
        {
            result[loop] = (String)nodes.elementAt(loop);
            System.out.println(result[loop]);
        }

    }
   return result;
}
}
